package com.Rest2Go;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.Rest2Go.Utils.SimplePhotoCache;
import com.Rest2Go.Utils.Utils;

public class RestaurantActivity extends ActivityWrapper {

	private long mRestId;
	private Button menuButton;
	private long mId;
	private String name;
	private SimplePhotoCache photos;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("RONI", "RestaurantActivity");

		Intent intent = getIntent();
		mContext = getBaseContext();

		long restId = intent.getLongExtra(Consts.Extras.REST_ID, -1);

		if (restId >= 0) {
			setContentView(R.layout.restaurant_layout);
			mRestId = restId;
//			Button backButton = (Button) findViewById(R.id.back_button);
//			backButton.setOnClickListener(new View.OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					//RestaurantActivity.this.finish();
//					Intent newRest = new Intent(mContext, NewRestActivity.class);
//					newRest.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					newRest.putExtra(Consts.Extras.REST_ID, mId);
//					newRest.putExtra(Consts.Extras.REST_NAME, name);
//					getBaseContext().startActivity(newRest);
//					
//				}
//			});
//			menuButton = (Button) findViewById(R.id.menu_button);
//			menuButton.setOnClickListener(new View.OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
////					Intent intent = new Intent(getBaseContext(), MenuActivity.class);
////					intent.putExtra(Consts.Extras.REST_ID, mId);
////
////					intent.putExtra(Consts.Extras.REST_NAME, name);
////					v.getContext().startActivity(intent);
//				}
//			});
			getRestDetails();
		} else {
			finish();
		}

	}

	private void getRestDetails() {
		new AsyncTask<Void, String, String>() {
			@Override
			protected void onPreExecute() {
				RestaurantActivity.this.showLoading(this);
			}

			@Override
			protected String doInBackground(Void... arg0) {
				return (Utils.preformRequest(Utils.BASE_URL + "GetRestDetails.php?id=" + mRestId));
			}

			@Override
			protected void onPostExecute(String body) {
				Utils.log(body);
				if (body != null) {
					JSONObject obj = null;
					try {
						obj = new JSONObject(body);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (obj != null) {
						JSONArray arr = obj.optJSONArray("restaurants");
						displayRestDetails(arr);
					} else {

					}
					removeDialog(ProgressDialog.STYLE_HORIZONTAL);

				}
			}

		}.execute();

	}

	protected void displayRestDetails(JSONArray arr)
	{
		photos = new SimplePhotoCache(getBaseContext().getResources().getDrawable(R.drawable.icon));
		for (int i = 0; i <= arr.length() - 1; i++)
		{
			JSONObject rest = arr.optJSONObject(i);
			String phone2 = "";
			String imageUrl = null;
			
			TextView restName = (TextView) findViewById(R.id.txtRestName);
			TextView restAddress = (TextView) findViewById(R.id.txtRestAddress);
			TextView restPhone = (TextView) findViewById(R.id.txtRestPhone);
			TextView restPhone2 = (TextView) findViewById(R.id.txtRestPhone2);
			ImageView restImage = (ImageView) findViewById(R.id.restImg);
			
			mId = rest.optLong(Consts.DB_COLUMNS.ID);
			name = rest.optString(Consts.DB_COLUMNS.NAME);
			String address = rest.optString(Consts.DB_COLUMNS.ADDRESS);
			String phone = rest.optString(Consts.DB_COLUMNS.PHONE);
			if (rest.optString(Consts.DB_COLUMNS.SECONDARY_PHONE) != null) phone2 = rest.optString(Consts.DB_COLUMNS.SECONDARY_PHONE);
			if (rest.optString(Consts.DB_COLUMNS.IMAGE_URL) != null)
			{
				imageUrl = rest.optString(Consts.DB_COLUMNS.IMAGE_URL);
			}
			if (TextUtils.isEmpty(imageUrl) || !imageUrl.equals("null"))
			{
				photos.loadPhoto(imageUrl, restImage);
			}
			

			restName.setText(name);
			restAddress.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + ((TextView) v).getText()));
					try
					{
						startActivity(intent);
					}
					catch (ActivityNotFoundException ex)
					{
						Toast.makeText(getBaseContext(), "Google Maps is not installed", Toast.LENGTH_LONG).show();
					}
				}
			});
			restAddress.setText(address);
			restPhone.setText(phone);
			Linkify.addLinks(restPhone, Linkify.PHONE_NUMBERS);
			Linkify.addLinks(restPhone2, Linkify.PHONE_NUMBERS);
			if (!TextUtils.isEmpty(phone2))
			{
				restPhone2.setText(phone2);
			}
			else
			{
				restPhone2.setVisibility(View.GONE);
			}
			

		}

	}
}
